<template>
  <div class="login">
    <div class="login__header">
      <h3 class="login__title">您好</h3>
      <h3 class="login__title">欢迎使用网龙多学</h3>
      <div class="login__tabs">
        <div @click="isPassword = true" :class="[isPassword && 'is--active']">密码登录</div>
        <div @click="isPassword = false" :class="[!isPassword && 'is--active']">验证码登录</div>
        <img :class="[isPassword ? 'l' : 'r', 'login__sj']" src="@/assets/images/lsj.png" />
      </div>
    </div>
    <div class="login__content">

      <van-cell-group :border="false">
        <div class="border-b van-hairline--bottom">
          <van-field :border="false" v-model="username" name="企业ID" placeholder="企业ID" />
        </div>
        <div class="border-b van-hairline--bottom">
          <van-field :border="false" v-model="account" type="account" name="账号" placeholder="账号或手机号" />
        </div>
        <div class="border-b van-hairline--bottom" v-if="isPassword">
          <van-field :border="false" v-model="password" type="password" name="密码" placeholder="密码" />
        </div>
        <div class="login__code van-hairline--bottom border-b" v-else>
          <van-field :border="false" v-model="code" name="验证码" placeholder="请输入验证码" />
          <span class="text">获取验证码</span>
        </div>
        <div class="login__btn" @click="show = true">
          登录
        </div>
        <div class="login__tip">
          <van-checkbox icon-size="14px" v-model="checked">
            <div class="login__xy">我已阅读并同意<span>《服务协议》</span>和<span>《隐私政策》</span></div>
          </van-checkbox>
        </div>
      </van-cell-group>
      <div class="forget text">忘记密码?</div>
    </div>

    <van-dialog v-model:show="show" :showConfirmButton="false">
      <div class="pop">
        <div class="pop__title">用户协议及隐私协议</div>
        <div class="pop__content">
          为了更好的保障您的合法权益，请您阅读并同意《服务协议》《隐私协议》《中国移动认证服务协议》
        </div>
        <div class="pop__subtitle">您需要同意并接受全部条款后开始我们的服务</div>
        <div class="pop__btns">
          <div class="pop__no pop__btn" @click="show = false">不同意</div>
          <div class="pop__agree pop__btn" @click="show = false">同意并继续</div>
        </div>
      </div>
    </van-dialog>
  </div>
</template>

<script setup>
import { ref } from "vue";
import { showSuccessToast, showFailToast } from "vant";
import router from "../../router/index";
import { useRoute } from "vue-router";
// import { appLogin } from "@/api/authApi.js";

const username = ref("");
const password = ref("");
const account = ref("");
const isPassword = ref(true)
const show = ref(false)


const check = ref(true);

const toRegister = () => {
  router.push("/register");
};

const onGoHome = () => {
  router.push({
    name: "home",
  });
};

const route = useRoute();
const onSubmit = async () => {
  if (!check.value) {
    return showFailToast("请勾选服务协议！");
  }

  // await appLogin(username.value, password.value);

  showSuccessToast("登录成功！");

  setTimeout(() => {
    if (route.meta._isEntry || `${route.query.isEntry}` === "true") {
      onGoHome();
    } else {
      router.go(-1);
    }
  }, 2000);
};
</script>
<script>
export default {
  beforeRouteEnter(to, from, next) {
    next(() => {
      to.meta._isEntry = from.fullPath === "/" || from.meta.noAfterLogin;
    });
  },
};
</script>

<style scoped lang="scss">
.login {
  height: 100vh;
  display: flex;
  flex-direction: column;
  background-image: url('@/assets/images/login.png');
  background-size: contain;

  &__header {
    padding: 51px 36px 0 51px;
  }

  &__content {
    flex: 1;
    margin-top: 36px;
    border-radius: 36px 36px 0 0;
    padding: 36px 36px;
    position: relative;

    .forget {
      position: absolute;
      bottom: 16px;
      left: 50%;
      transform: translateX(-50%);
      color: #0DC678;
    }
  }

  &__code {
    display: flex;
    align-items: center;


    .text {
      white-space: nowrap;
      color: #0DC678;
    }
  }

  &__tip {
    padding-top: 26px;
    display: flex;
    justify-content: center;
  }

  &__xy {
    font-family: PingFangSC-Regular;
    font-size: 12px;
    color: #B4B7BB;

    span {
      color: #0DC678;
    }
  }

  .border-b {
    margin-bottom: 10px;

    ::v-deep .van-cell {
      padding: 10px 0;
    }
  }

  &__title {
    margin: 0;
    font-family: PingFangSC-Medium;
    font-size: 24px;
    color: #272B2D;
    line-height: 36px;
  }

  &__btn {
    margin-top: 37px;
    padding: 13px 0;
    font-family: PingFangSC-Regular;
    font-size: 17px;
    color: #FFFFFF;
    text-align: center;
    background-image: linear-gradient(270deg, #1CA893 0%, #1BCD74 100%);
    border-radius: 22px;
  }

  &__tabs {
    padding-top: 26px;
    display: flex;
    align-items: center;
    position: relative;

    div {
      flex: 1;
      text-align: center;
      font-family: PingFangSC-Regular;
      font-size: 14px;
      color: #676B6B;

      &.is--active {
        color: #272B2D;
      }
    }
  }

  &__sj {
    position: absolute;
    bottom: -36px;
  
    width: 99px;
    height: 24px;
    transform: translateX(-50px);
    &.l{
      left: 25%;
    }
    &.r{
      left: 75%;
    }
  }

  &__content {
    background-color: #fff;
  }

  .pop {
    padding: 32px 24px;

    &__title {
      font-family: Helvetica;
      font-size: 18px;
      color: #27273F;
      letter-spacing: 0;
      text-align: center;
      line-height: 27px;
      margin-bottom: 12px;
    }

    &__content {
      font-family: PingFangSC-Regular;
      font-size: 14px;
      color: #000000;
      letter-spacing: 0;
      text-align: center;
      line-height: 21px;
      margin-bottom: 24px;
      text-align: justify;
    }

    &__subtitle {
      font-family: Helvetica;
      font-size: 14px;
      color: #9898AE;
      letter-spacing: 0;
      line-height: 21px;
    }

    &__btns {
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding-top: 12px;
    }

    &__btn {
      background: rgba(0, 0, 0, 0.04);
      border-radius: 12px;
      width: 48%;
      height: 48px;
      display: flex;
      align-items: center;
      justify-content: center;
      font-family: PingFangSC-Regular;
      font-size: 16px;
      color: #515167;
      letter-spacing: 0;
      line-height: 24px;
    }

    &__agree {
      color: #FFFFFF;
      background-image: linear-gradient(270deg, #00C37D 49%, #54D781 100%);
    }
  }
}</style>
